package cn.mesmile.security.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.mesmile.security.domain.Employee;
import cn.mesmile.security.domain.Role;
import cn.mesmile.security.dto.EmployeeInfo;
import cn.mesmile.security.mapper.EmployeeMapper;
import cn.mesmile.security.mapper.RoleMapper;
import cn.mesmile.security.service.IEmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zb
 * @date 2020/4/5 16:05
 * @Description:
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {


    private final EmployeeMapper employeeMapper;

    private final RoleMapper roleMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        EmployeeInfo employeeInfo = getEmpByUsername(username);
        if(employeeInfo ==  null || StrUtil.isEmpty(employeeInfo.username)) {
            throw new UsernameNotFoundException("用户名不存在");
        }
        // 定义权限列表
        List<GrantedAuthority> authorities = new ArrayList<>();
        // 用户可以访问的资源名称   注意：必须"ROLE_"开头
        employeeInfo.roles.forEach(role -> {
            String keyword = role.getKeyword();
            authorities.add(new SimpleGrantedAuthority(keyword));
        });

        /**
         *
         *  记住我功能： name="remember-me" value="true"
         * String username,
         * String password,
         * boolean enabled, 是否可用                    默认为 true
         * boolean accountNonExpired, 账户是否失效      默认为 true
         * boolean credentialsNonExpired, 密码是否失效  默认为 true
         * boolean accountNonLocked, 账户是否锁定       默认为 true
         * Collection<? extends GrantedAuthority> authorities 角色
         */
        return new User(username,
                employeeInfo.password,
                employeeInfo.status,
                true,
                true,
                true,
                authorities);
    }


    @Autowired
    public EmployeeServiceImpl(EmployeeMapper employeeMapper, RoleMapper roleMapper) {
        this.employeeMapper = employeeMapper;
        this.roleMapper = roleMapper;
    }


    @Override
    public EmployeeInfo getEmpByUsername(String username) {
        EmployeeInfo employeeInfo = new EmployeeInfo();
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(StrUtil.isNotBlank(username),Employee::getUsername,username);
        Employee employee = employeeMapper.selectOne(lambdaQueryWrapper);
        String eid = employee.getId();
        List<Role> roles = roleMapper.findByEid(eid);
        BeanUtils.copyProperties(employee,employeeInfo);
        employeeInfo.setRoles(roles);
        return employeeInfo;
    }



}
